package com.hk.service.order.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.hk.commons.entity.Parameter;
import com.hk.commons.utils.yingmi.YingmiApiUtils;
import com.hk.service.order.IParameter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * 盈米华康宝
 * @author Evan
 * @date 2017/11/10
 */
@Service
public class YingMiWalletImpl implements Callable<Map<String,Object>>{
    private Logger log = LoggerFactory.getLogger(YingMiWalletImpl.class);

    @Autowired
    private YingmiApiUtils yingMiService;

    private String brokerUserId;

    private String accountId;

    @Override
    public Map<String, Object> call() throws Exception {
        Map<String, Object> wallet=new HashMap<>(2);
        wallet.put("allYingMiWallet", BigDecimal.valueOf(0.00));
        wallet.put("yingMiWalletConfirmAllProfit", BigDecimal.valueOf(0.00));
        if(StringUtils.isBlank(brokerUserId) || StringUtils.isBlank(accountId)){
            log.info("用户accountId ={},name={} 为空",accountId,brokerUserId);
            return wallet;
        }
        getAllWallet(wallet);
        return wallet;
    }


    /**
     * 获取总资产和累计收益
     * @return
     */
    private void  getAllWallet(Map<String,Object> wallet){
        final BigDecimal[] result = {BigDecimal.valueOf(0.00),BigDecimal.valueOf(0.00)};
        try {
            Map<String, String> params = new HashMap<>(3);
            params.put("brokerUserId", brokerUserId);
            params.put("accountId", accountId);
            String response = yingMiService.get("/trade/getWalletShares", params);
            if(response.contains("msg")){
                log.error("获取华康宝总资产异常！={}",response);
                return ;
            }
            List<Map<String,Object>> list = JSON.parseObject(response,new TypeReference<List<Map<String,Object>>>(){});
            list.stream()
                    .forEach(m ->{
                        result[0] = result[0].add(new BigDecimal(m.get("totalAvailShare").toString()));
                        if(m.get("accumulatedProfit") != null && StringUtils.isNotBlank(String.valueOf(m.get("accumulatedProfit")))){
                            result[1] = result[1].add(new BigDecimal(m.get("accumulatedProfit").toString()));
                        }
                    } );
        }catch (Exception e){
            log.error("查询华康宝总资产异常",e);
        }
        wallet.put("allYingMiWallet",result[0]);
        wallet.put("yingMiWalletConfirmAllProfit",result[1]);
    }

    public String getBrokerUserId() {
        return brokerUserId;
    }

    public void setBrokerUserId(String brokerUserId) {
        this.brokerUserId = brokerUserId;
    }

    public String getAccountId() {
        return accountId;
    }

    public void setAccountId(String accountId) {
        this.accountId = accountId;
    }

}
